package com.net
{
 
	
	import flash.events.ErrorEvent;
	import flash.events.Event;
	import flash.events.NetStatusEvent;
	import flash.events.ProgressEvent;
	import flash.events.SecurityErrorEvent;
	import flash.net.Socket;
	import flash.utils.ByteArray;
	
	
	
	public class Client
	{
		 private  var socket:Socket=new Socket();
	 
         private var main:Main;
 
         public function Client(){
			 
		 }
		 public  function setGameEntry(g:Main):void{
			 main=g;
		 }	
		 public  function connectLoginServer(ip:String,port:int):void{
		 	try{
			 	disconFromLoginServer();
				disconFromGameServer();
		   
				//监听连接成功事件
				socket.addEventListener(Event.CONNECT,connectLoginServerOK);
				//监听关闭事件
				socket.addEventListener(Event.CLOSE,loginClose);
				//监听服务器新信息
				socket.addEventListener(ProgressEvent.SOCKET_DATA,socketData);
				
				socket.addEventListener(ErrorEvent.ERROR, loginClose);

				
				socket.connect(ip,port);//执行连接
		 	}catch(e:TypeError){
				loginClose(null);
		 	}
		 }
		 
	 	 public  function connectGameServer(ip:String,port:int):void{
	 	 	try{
				disconFromLoginServer();
				disconFromGameServer();   
		 
				//监听连接成功事件
				socket.addEventListener(Event.CONNECT,connectGameServerOK);
				//监听关闭事件
				socket.addEventListener(Event.CLOSE,gameClose);
				//监听服务器新信息
				socket.addEventListener(ProgressEvent.SOCKET_DATA,socketData);
				
				socket.addEventListener(ErrorEvent.ERROR, gameClose);

				
				socket.connect(ip,port);//执行连接
			}catch(e:TypeError){
		 		gameClose(null);
		 	}
		 }
	 
		 public  function disconFromLoginServer():void{
		 	if(socket.connected){
				socket.close();
		 	}
			socket.removeEventListener(Event.CONNECT,connectLoginServerOK);
			socket.removeEventListener(Event.CLOSE,loginClose);
			socket.removeEventListener(ProgressEvent.SOCKET_DATA,socketData);
			socket.removeEventListener(ErrorEvent.ERROR, loginClose);
		 }
		 public  function disconFromGameServer():void{
			 if(socket.connected){
				 socket.close();
			 }
			 socket.removeEventListener(Event.CONNECT,connectGameServerOK);
			 socket.removeEventListener(Event.CLOSE,gameClose);
			 socket.removeEventListener(ProgressEvent.SOCKET_DATA,socketData);
			 socket.removeEventListener(ErrorEvent.ERROR, gameClose);
		 }
		 
	
		 private  function loginClose(event:Event):void
		 { 
			 disconFromLoginServer();
		 	 main.connectError();
		 }
		 private  function gameClose(event:Event):void
		 { 
			 disconFromGameServer();
			 main.connectError();
		 }
 		 
 
		 private   function connectLoginServerOK(event:Event):void
		 {
			 main.connectLoginServerOk();
		 }
		 private  function connectGameServerOK(event:Event):void
		 {
			 main.connectGameServerOK();
		 }
	
		 private  function socketData(event:ProgressEvent):void
		 {
			 
		        var message:ByteArray=new ByteArray();
				//循环读取数据，socket的bytesAvailable对象存放了服务器传来的所有数据
		      
		        while(socket.bytesAvailable)
		        {
		        	socket.readBytes(message);
		        }
		        NetTrans.setData(message);  
			 
	    }
 
	    public   function sendMessage(message:ByteArray):void //发送数据到服务器 
	    {
			//新建一个ByteArray来存放数据
			// var message:ByteArray=new ByteArray();
			//写入数据，使用writeUTFBytes以utf8格式传数据，避免中文乱码
			// message.writeInt(987654);
			//写入socket的缓冲区
		 	socket.writeInt(message.length);
			socket.writeBytes(message); 
		 
			//调用flush方法发送信息
			socket.flush();
			//trace("flush");
			//清空消息框
			
		}
 
	}
}